package com.example.jones.test;

import org.json.JSONArray;
import org.json.JSONException;

import com.example.util.HttpUtil;
import com.example.util.HttpUtilWithParameter;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MatchView {
	private Context mcontext = null;
	private ListView match_list = null;
	private View mView;
	ListAdepter lap;
	ImageView  iv_search;
	TextView et_key,t1;
	
	public MatchView(Context c) {
		this.mcontext = c;
	}

	/**
	 * 返回布局原料配菜
	 * @throws Exception 
	 * @throws JSONException 
	 */
	public View getView() throws JSONException, Exception {

		mView = View.inflate(mcontext, R.layout.match, null);
		match_list = (ListView) mView.findViewById(R.id.match_list);	//查找的结果list
		iv_search=(ImageView) mView.findViewById(R.id.iv_search);		//搜索的按钮
		et_key=(TextView) mView.findViewById(R.id.et_key);				//搜索的编辑框
		t1=(TextView) mView.findViewById(R.id.t1);						//title:原料配菜
		t1.setText("原料配菜");
		
		
		String url = HttpUtil.Base_url+"/DailyRecommendService";
		JSONArray match_jsonArray = new JSONArray(HttpUtil.getRequest(url));
//		lap=new ListAdepter(handler,match_jsonArray,mcontext);
//		match_list.setAdapter(lap);

		iv_search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(mcontext, "点击了搜索！", 1000).show();
				String url = HttpUtil.Base_url+"/MatchService";
				JSONArray match_jsonArray;
				try {
					match_jsonArray = new JSONArray(HttpUtilWithParameter.getCookMethod(url, et_key.getText().toString()));
					System.out.println("##########点击了搜索，match_jsonArray："+match_jsonArray);
					lap=new ListAdepter(handler,match_jsonArray,mcontext);
					match_list.setAdapter(lap);
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		return mView;
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(mcontext, "点击了评论！", Toast.LENGTH_LONG).show();
				break;
			case 1:
				Toast.makeText(mcontext, "点击了收藏！", Toast.LENGTH_LONG).show();
				break;
			case 2:
				Intent in=new Intent();
				in.setClass(mcontext, DtlActivity.class);
				mcontext.startActivity(in);
				break;
			default:
				break;
			}
		};
	};
}